﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMS.DB
{
    class InvoicesMng : DBController
    {
        public List<Invoice> GetInvoiceById(string id)
        {
            var listItems = new List<Invoice>();
            try
            {
                IOrderedQueryable<Invoice> query = from item in DataEntities.Invoices
                                                   where item.SaleInvoiceNo.ToLower().Contains(id.ToLower())
                                                   orderby item.Date descending
                                                   select item;

                listItems = query.ToList();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return listItems;
        }
        public List<Invoice> GetDraftInvoices()
        {
            var listItems = new List<Invoice>();
            try
            {
                IQueryable<Invoice> query = from item in DataEntities.Invoices
                                            where item.IsDraft == true
                                            orderby item.Date descending
                                            select item;

                listItems = query.ToList();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return null;
            }

            return listItems;
        }
        public List<Invoice> GetDraftInvoices(string productId)
        {
            var listItems = new List<Invoice>();
            try
            {
                listItems = GetDraftInvoices();
                IOrderedEnumerable<Invoice> query = from item in listItems
                                                    where item.SaleInvoiceNo.ToLower().Contains(productId.ToLower())
                                                    orderby item.Date descending
                                                    select item;

                listItems = query.ToList();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return null;
            }

            return listItems;
        }
        public List<Invoice> GetCompleteInvoices()
        {
            var listItems = new List<Invoice>();
            try
            {
                IQueryable<Invoice> query = from item in DataEntities.Invoices
                                            where item.IsDraft == false
                                            orderby item.Date descending
                                            select item;

                listItems = query.ToList();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return null;
            }

            return listItems;
        }
        public List<Invoice> GetInvoiceByCusCode(string id)
        {
            List<Invoice> item = new List<Invoice>();
            try
            {
                IQueryable<Invoice> query = from _item in DataEntities.Invoices
                                            where _item.CustomerID.ToLower().Contains(id.ToLower())
                                            select _item;

                item = query.ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return null;
            }
            return item;
        }

        public Invoice GetItemById(string orderCode)
        {
            var item = new Invoice();
            try
            {
                IQueryable<Invoice> query = from _item in DataEntities.Invoices
                                            where _item.OrderCode.Equals(orderCode)
                                            select _item;

                item = query.FirstOrDefault();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return null;
            }
            return item;
        }
        public Invoice GetItemBySaleInvoiceNo(string invoiceNo)
        {
            var item = new Invoice();
            try
            {
                IQueryable<Invoice> query = from _item in DataEntities.Invoices
                                            where _item.SaleInvoiceNo.ToLower().Equals(invoiceNo.ToLower())
                                            select _item;

                item = query.FirstOrDefault();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return null;
            }
            return item;
        }

        public double[] CalculateRevenuePerMonth(int month, int year)
        {
            double[] revenue = new double[2];
            revenue[0] = 0;
            revenue[1] = 0;
            try
            {
                int days = DateTime.DaysInMonth(year, month);
                var fDate = new DateTime(year, month, 1, 0, 0, 0);
                var tDate = new DateTime(year, month, days, 23, 59, 59);

                var result = from item in DataEntities.Invoices
                             where (item.Date >= fDate & item.Date <= tDate)
                             select new
                             { income = item.TotalExportCost,
                             cost = item.TotalImportCost };

                foreach (var item in result)
                {
                    revenue[0] += (double)item.income;
                    revenue[1] += (double)item.income - (double)item.cost;
                }
                return revenue;
            }
            catch (Exception)
            {
                return revenue;
            }
        }

        public double[] GetInvoiceByDate(DateTime fDate, DateTime tDate)
        {
            double[] data = new double[2];
            //var listItems = new List<Invoice>();
            try
            {
                double incomeGold = 0;
                double costGold = 0;
                var result = from item in DataEntities.Invoices
                             where (item.Date >= fDate && item.Date <= tDate)
                             select new
                             { income = item.TotalExportCost,
                             cost = item.TotalImportCost };
                foreach (var item in result)
                {
                    incomeGold += (double)item.income;
                    costGold += (double)item.cost;
                }
                data[0] = incomeGold;
                data[1] = costGold;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }
            return data;
        }
        public bool UpdateItem(string orderCode, string customerId, string saleInvoiceNo, decimal totalImportCost, decimal totalExportCost, DateTime date, bool isDraft)
        {
            try
            {
                var item = new Invoice();

                IQueryable<Invoice> query = from _item in DataEntities.Invoices
                                            where _item.OrderCode.Equals(orderCode)
                                            select _item;

                item = query.FirstOrDefault();

                if (item == null)
                {
                    return false;
                }

                item.CustomerID = customerId;
                item.SaleInvoiceNo = saleInvoiceNo;
                item.IsDraft = isDraft;
                item.TotalImportCost = totalImportCost;
                item.TotalExportCost = totalExportCost;
                item.Date = date;

                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
        public bool IsExist(string orderCode)
        {
            try
            {
                var query = from _item in DataEntities.Invoices
                            where _item.OrderCode.Equals(orderCode)
                            select _item;

                var item = query.FirstOrDefault();
                if (item == null)
                    return false;
                var output = DataEntities.Invoices.Where(a => a.OrderCode == item.OrderCode);
                return output != null && output.Count() > 0 ? true : false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
        public bool AddNewItems(string customerId, string saleInvoiceNo, string orderCode, decimal totalImportCost, decimal totalExportCost, DateTime date, string type, bool isDraft, decimal discount)
        {
            try
            {
                var item = new Invoice { CustomerID = customerId,
                SaleInvoiceNo = saleInvoiceNo,
                OrderCode = orderCode,
                TotalImportCost = totalImportCost,
                TotalExportCost = totalExportCost,
                Date = date,
                ProductType = type,
                IsDraft = isDraft,
                Discount = discount };
                DataEntities.AddToInvoices(item);
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }

        public bool DeleteItem(string id)
        {
            try
            {
                var item = GetItemById(id);
                if (item == null)
                    return false;
                DataEntities.DeleteObject(item);
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }

        public bool DeleteAll()
        {
            try
            {
                var list = DataEntities.Invoices.ToList();
                foreach (var item in list)
                {
                    DataEntities.Invoices.DeleteObject(item);
                }

                DataEntities.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }

            return true;

        }
    }
}
